package com.wandian.util
{

	import com.wandian.util.Res;
	import com.wandian.view.ui.AnimationButton;
	import com.wandian.view.ui.Button;
	import com.wandian.view.ui.SimpleButton;
	import com.wandian.view.ui.TextButton;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getQualifiedClassName;

	/**
	 *
	 * 界面帮助类
	 * @author long
	 *
	 */
	public class UIHelper
	{
		/**
		 * 通过指定图片类名字，给Sprite添加图片
		 * @param parentSprite
		 * @param bitmapName
		 * @param sx
		 * @param sy
		 * @param boolVisible
		 */
		public static function addBitmapChild(parentSprite:Sprite, bitmapName:String, sx:int=0, sy:int=0, boolVisible:Boolean=true):Bitmap
		{
			if (!parentSprite || !bitmapName)
			{
				return null;
			}
			var bitmap:Bitmap=new Bitmap(Res.get(bitmapName));
			bitmap.x=sx;
			bitmap.y=sy;
			bitmap.visible=boolVisible;
			parentSprite.addChild(bitmap);
			return bitmap;
		}

		/**
		 * 添加一个可移动位置的button
		 * @param parentSprite
		 * @param key
		 * @param func
		 * @param sx
		 * @param sy
		 * @param boolVisible
		 * @return
		 */
		public static function addBAnimationButtonChild(parentSprite:Sprite, key:String, func:Function=null, sx:int=0, sy:int=0, dx:int=0, dy:int=0, boolVisible:Boolean=true):AnimationButton
		{
			// TODO Auto Generated method stub
			var btn:AnimationButton=new AnimationButton(key, {x: sx, y: sy}, {x: sx + dx, y: sy + dy});
			btn.visible=boolVisible;
			parentSprite.addChild(btn);
			if (func != null)
			{
				btn.setClickHandler(func);
			}
			return btn;
		}

		public static function addBitmapButtonChild(parentSprite:Sprite, key:String, func:Function=null, sx:int=0, sy:int=0, boolVisible:Boolean=true):SimpleButton
		{
			// TODO Auto Generated method stub
			var btn:SimpleButton=new SimpleButton(key);
			btn.x=sx;
			btn.y=sy;
			btn.visible=boolVisible;
			btn.useHandCursor=true;
			parentSprite.addChild(btn);
			if (func != null)
			{
				btn.addEventListener(MouseEvent.CLICK, func);
			}
			return btn;
		}

		/**
		 * 绘制一个矩形精灵到指定的父容器里
		 * @param parentSprite
		 * @param childSprite
		 * @param childName
		 * @param posx
		 * @param posy
		 * @param swidht
		 * @param sheight
		 * @param color
		 * @param alpha
		 * @param sx
		 * @param sy
		 * @param boolButtonMode
		 * @param boolVisible
		 */
		public static function addRectSpriteChild(parentSprite:Sprite, childSprite:Sprite, childName:String=null, posx:int=0, posy:int=0, swidht:int=0, sheight:int=0, color:uint=0x000000, alpha:Number=1, sx:int=0, sy:int=0, boolButtonMode:Boolean=false, boolVisible:Boolean=true):Sprite
		{
			// TODO Auto Generated method stub
			if (!childSprite)
			{
				childSprite=new Sprite();
			}
			if (childName && parentSprite.getChildByName(childName))
			{
				childSprite.name=childName;
			}
			childSprite.x=posx;
			childSprite.y=posy;
			childSprite.graphics.beginFill(color, alpha);
			childSprite.graphics.drawRect(sx, sy, swidht, sheight);
			childSprite.graphics.endFill();
			childSprite.buttonMode=boolButtonMode;
			parentSprite.addChild(childSprite);
			return childSprite;
		}

		public static function addRectMaskSpriteChild(parentSprite:Sprite, alpha:Number=0.2, color:uint=0x008B00, posx:int=0, posy:int=0, sx:int=0, sy:int=0, boolButtonMode:Boolean=false, boolVisible:Boolean=true):Sprite
		{
			// TODO Auto Generated method stub
			var childSprite:Sprite=new Sprite();
			if (!childSprite)
			{
				childSprite=new Sprite();
			}
			childSprite.x=posx;
			childSprite.y=posy;
			childSprite.graphics.beginFill(color, alpha);
			childSprite.graphics.drawRect(sx, sy, parentSprite.width, parentSprite.height);
			childSprite.graphics.endFill();
			childSprite.buttonMode=boolButtonMode;
			parentSprite.addChild(childSprite);
			return childSprite;
		}

		/**
		 * 初始化一个精灵到指定的父容器里
		 * @param parentSprite
		 * @param childSprite
		 * @param childName
		 * @param posx
		 * @param posy
		 * @param boolButtonMode
		 * @param boolVisible
		 */
		public static function addSpriteChild(parentSprite:Sprite, childSprite:Sprite, childName:String=null, posx:int=0, posy:int=0, boolButtonMode:Boolean=false, boolVisible:Boolean=true):Sprite
		{
			// TODO Auto Generated method stub
			if (!childSprite)
			{
				childSprite=new Sprite();
			}
			if (childName && parentSprite.getChildByName(childName))
			{
				childSprite.name=childName;
			}
			childSprite.x=posx;
			childSprite.y=posy;
			childSprite.buttonMode=boolButtonMode;
			childSprite.visible=boolVisible;
			parentSprite.addChild(childSprite);
			return childSprite;
		}

		public static function addTextFieldChild(parentSprite:Sprite, textMsg:String="", childSprite:TextField=null, posx:int=0, posy:int=0, _selectable:Boolean=true, _textFormat:TextFormat=null, filters:Array=null, boolVisible:Boolean=true):TextField
		{
			// TODO Auto Generated method stub
			if (!childSprite)
			{
				childSprite=new TextField();
			}

			childSprite.x=posx;
			childSprite.y=posy;
			childSprite.visible=boolVisible;
			childSprite.selectable=_selectable;
			childSprite.text=textMsg;
			//childSprite.autoSize=TextFieldAutoSize.CENTER;
			childSprite.mouseEnabled=false;
			if (_textFormat)
				childSprite.setTextFormat(_textFormat);
			if (filters)
				childSprite.filters=filters;
			parentSprite.addChild(childSprite);
			return childSprite;
		}


		public static function addBitmapDataButtonChild(parentSprite:Sprite, downUrl:Class, overUrl:Class, func:Function=null, sx:int=0, sy:int=0, boolButton:Boolean=false, boolVisible:Boolean=true):Button
		{
			if (!parentSprite || !downUrl)
			{
				return null;
			}
			var btn:Button=new Button(downUrl, overUrl, null, boolButton);
			btn.x=sx;
			btn.y=sy;
			btn.visible=boolVisible;
			if (func != null)
			{
				btn.setClickHandler(func);
			}
			parentSprite.addChild(btn);
			return btn;
		}

		public static function addBitmapDataTextButtonChild(parentSprite:Sprite, btntext:String, downUrl:Class, overUrl:Class, boolButton:Boolean=false, func:Function=null, tx:int=0, ty:int=0, sx:int=0, sy:int=0, boolVisible:Boolean=true):TextButton
		{
			if (!parentSprite || !downUrl)
			{
				return null;
			}
			var btn:TextButton=new TextButton(btntext, downUrl, overUrl, null, boolButton, tx, ty);
			btn.x=sx;
			btn.y=sy;
			btn.visible=boolVisible;
			if (func != null)
			{
				btn.setClickHandler(func);
			}
			parentSprite.addChild(btn);
			return btn;
		}

		/**
		 * 通过指定swc中图片类，给Sprite添加图片
		 * @param parentSprite 需要显示在的容器
		 * @param bitmapdataclz 图片资源类
		 * @param sx
		 * @param sy
		 * @param boolVisible 是否可见
		 */
		public static function addBitmapDataChild(parentSprite:Sprite, bitmapdataclz:Class, sx:int=0, sy:int=0, boolVisible:Boolean=true):void
		{
			if (!parentSprite || !bitmapdataclz)
			{
				return;
			}
			var bitmap:Bitmap=new Bitmap(new bitmapdataclz as BitmapData);
			bitmap.x=sx;
			bitmap.y=sy;
			bitmap.visible=boolVisible;
			parentSprite.addChild(bitmap);
		}

	}
}
